package com.xuzhen.tarena;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 达内培训时五子棋练习
 */
public class WuZiQi {
	private char[][] ch = new char[16][16];

	{
		for (int i = 0; i < ch.length; i++) {
			for (int j = 0; j < ch[i].length; j++) {
				ch[i][j] = '*';
			}
		}
	}

	// 棋盘
	public void printChessBoard() {
		System.out.print("\t\t");
		for (int i = 0; i < 16; i++) {
			if (i < 10) {
				System.out.print(i + "\t\t");
			} else {
				System.out.print((char) ('a' + i - 10) + "\t\t");
			}
		}
		System.out.println();
		System.out.println();
		for (int i = 0; i < ch.length; i++) {
			if (i < 10) {
				System.out.print(i + "\t\t");
			} else {
				System.out.print((char) ('a' + i - 10) + "\t\t");
			}
			for (int j = 0; j < ch[i].length; j++) {
				System.out.print(ch[i][j] + "\t\t");
			}
			System.out.println();
			System.out.println();
		}
	}

	// 落子
	public boolean in(char s) {
		System.out.println("请" + s + "方落子");
		Scanner scan = new Scanner(System.in);
		String str = scan.next();
		char[] c = new char[2];
		if (str.matches("[\\da-fA-F]{2}")) {
			str = str.toLowerCase();
			c = str.toCharArray();
		} else {
			System.out.println(s + "方输入错误，请重新输入");
			return false;
		}
		int[] index = new int[2];
		for (int i = 0; i < c.length; i++) {
			if (c[i] >= '0' && c[i] <= '9') {
				index[i] = c[i] - 48;
			} else {
				index[i] = c[i] - 87;
			}
		}
		if (ch[index[0]][index[1]] == '*') {
			ch[index[0]][index[1]] = s;
			if (win(index, s)) {
				System.out.println(s + "方胜");
				printChessBoard();
				System.exit(0);
			}
		} else {
			System.out.println("对应坐标有棋子，请" + s + "重新落子");
			return false;
		}
		return true;
	}

	// 开始
	public void start() {
		char[] c = new char[2];
		c[0] = '黑';
		c[1] = '白';
		int i = (int) (Math.random() * 2);
		System.out.println(c[i] + "方先下");
		while (true) {
			if (in(c[i])) {
				printChessBoard();
				i++;
				i = i % 2;
			}
		}
	}

	// 落下一子后，判断是否赢
	public boolean win(int[] index, char s) {
		int i = index[0];
		int j = index[1];
		StringBuilder sb = new StringBuilder();
		int[] k = new int[] { 4, 4, 4, 4 };
		// 判断列是否满足
		while (!(i - k[0] >= 0)) {
			k[0]--;
		}
		while (!(15 - i >= k[1])) {
			k[1]--;
		}
		for (int t = i - k[0]; t <= i + k[1]; t++) {
			sb.append(ch[t][j]);
		}
		if (s == '黑') {
			Pattern p = Pattern.compile("黑{5}");
			Matcher m = p.matcher(sb.toString());
			if (m.find()) {
				return true;
			}
		} else if (s == '白') {
			Pattern p = Pattern.compile("白{5}");
			Matcher m = p.matcher(sb.toString());
			if (m.find()) {
				return true;
			}
		}
		// 判断行是否满足
		sb = new StringBuilder();
		while (!(j - k[2] >= 0)) {
			k[2]--;
		}
		while (!(15 - j >= k[3])) {
			k[3]--;
		}
		for (int t = j - k[2]; t <= j + k[3]; t++) {
			sb.append(ch[i][t]);
		}
		if (s == '黑') {
			Pattern p = Pattern.compile("黑{5}");
			Matcher m = p.matcher(sb.toString());
			if (m.find()) {
				return true;
			}
		} else if (s == '白') {
			Pattern p = Pattern.compile("白{5}");
			Matcher m = p.matcher(sb.toString());
			if (m.find()) {
				return true;
			}
		}
		// 判断左上至右下是否满足
		sb = new StringBuilder();
		int leftup = 0;
		int rightdown = 0;
		if (k[0] >= k[2]) {
			leftup = k[2];
		} else {
			leftup = k[0];
		}
		if (k[1] >= k[3]) {
			rightdown = k[3];
		} else {
			rightdown = k[1];
		}
		for (int t = i - leftup, d = j - leftup; t <= i + leftup
				&& d <= j + rightdown; t++, d++) {
			sb.append(ch[t][d]);
		}
		if (s == '黑') {
			Pattern p = Pattern.compile("黑{5}");
			Matcher m = p.matcher(sb.toString());
			if (m.find()) {
				return true;
			}
		} else if (s == '白') {
			Pattern p = Pattern.compile("白{5}");
			Matcher m = p.matcher(sb.toString());
			if (m.find()) {
				return true;
			}
		}
		// 判断左下至右上是否满足
		sb = new StringBuilder();
		int leftdown = 0;
		int rightup = 0;
		if (k[0] >= k[2]) {
			leftup = k[2];
		} else {
			leftup = k[0];
		}
		if (k[1] >= k[3]) {
			rightdown = k[3];
		} else {
			rightdown = k[1];
		}
		for (int t = i - leftdown, d = j - rightup; t <= i + leftdown
				&& d <= j + rightup; t++, d++) {
			sb.append(ch[t][d]);
		}
		if (s == '黑') {
			Pattern p = Pattern.compile("黑{5}");
			Matcher m = p.matcher(sb.toString());
			if (m.find()) {
				return true;
			}
		} else if (s == '白') {
			Pattern p = Pattern.compile("白{5}");
			Matcher m = p.matcher(sb.toString());
			if (m.find()) {
				return true;
			}
		}

		return false;

	}

	public static void main(String[] args) {
		WuZiQi wzq = new WuZiQi();
		wzq.start();
	}

}
